package com.aifund.app.service.impl;

import com.aifund.app.entity.form.tactices.FollowExchangeManTacticsForm;
import com.aifund.app.entity.form.tactices.FollowRobotForm;
import com.aifund.app.entity.vo.UserAndExchangeKey;
import com.aifund.app.service.*;
import com.aifund.common.dao.FollowOrderManTacticsDao;
import com.aifund.common.entity.*;
import com.aifund.common.enums.DocumentaryInfo.FollowOrderEnum;
import com.aifund.common.enums.DocumentaryInfo.FollowTypeEnum;
import com.aifund.common.enums.redis.RedisStringKeyEnum;
import com.aifund.common.exception.AiFundException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class FollowOrderManTacticsServiceImpl implements FollowOrderManTacticsService {

    @Autowired
    FollowOrderManTacticsDao followOrderManTacticsDao;
    @Autowired
    UserService userService;
    @Autowired
    ExchangeManTacticsService exchangeManTacticsService;
    @Autowired
    FollowOrderExchangeService followOrderExchangeService;
    @Autowired
    FollowOrderCurrencyService followOrderCurrencyService;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public List<UserAndExchangeKey> getUserIdsByExchangeManId(Long exchangeManId, Integer status) {
        return null;
    }

    @Override
    public int getCountByExchangeManTacticsId(Long exchangeManTacticsId) {
        Integer countByExchangeManIdAndStatus = followOrderManTacticsDao.getAllByExchangeManTacticsIdAndStatus(exchangeManTacticsId, FollowOrderEnum.FOLLOWING.getStatus()).size();
        return countByExchangeManIdAndStatus!=null?countByExchangeManIdAndStatus:0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void followExchangeManTactics(FollowExchangeManTacticsForm form, Long userId) {

        /**
         * 校验策略
         */
        ExchangeManTactics exchangeManTactics  = exchangeManTacticsService.getById(form.getExchangeManTacticsId());
        if(exchangeManTactics == null){
            throw new AiFundException("500","不存在的策略");
        }

        /**
         * 保存跟单基本信息
         */
        FollowOrderManTactics followOrderMan = new FollowOrderManTactics();
        BeanUtils.copyProperties(form,followOrderMan);
        followOrderMan.setUserId(userId);
        followOrderMan.setStatus(FollowOrderEnum.FOLLOWING.getStatus());
        FollowOrderManTactics saveFollowOrder = followOrderManTacticsDao.save(followOrderMan);

        /**
         * 保存跟单交易所信息
         */
         for(Long exchangeId : form.getExchangeIds()){
             FollowOrderExchange followOrderExchange = new FollowOrderExchange();
             followOrderExchange.setFollowId(saveFollowOrder.getId());
             followOrderExchange.setExchangeId(exchangeId);
             followOrderExchange.setFollowType(FollowTypeEnum.EXCHANGE_MAN_FOLLOW.getType());
             followOrderExchangeService.save(followOrderExchange);
         }

        /**
         * 保存跟单币种信息
         */
        List<String> currencys = form.getCurrencys();
        for(String currency : currencys){
            FollowOrderCurrency followOrderCurrency = new FollowOrderCurrency();
            followOrderCurrency.setCurrency(currency);
            followOrderCurrency.setFollowId(saveFollowOrder.getId());
            followOrderCurrency.setFollowType(FollowTypeEnum.EXCHANGE_MAN_FOLLOW.getType());
            followOrderCurrencyService.save(followOrderCurrency);
        }


        redisTemplate.opsForValue().set(RedisStringKeyEnum.TACTICS_FOLLOW_INFO.getKey(),"");

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void followRobot(FollowRobotForm form, Long userId) {

    }
}
